package design.simdi.code.final_paper.site.panels.blog.trimise;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.entities.Mesaj;
import design.simdi.code.final_paper.core.services.ApplicationService;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.site.panels.blog.ViewMesajPanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;
import design.simdi.code.final_paper.utils.ToolbarItem;
import design.simdi.code.final_paper.utils.YesNoModalWindow;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 1:42:48 PM)
 * 
 */
public class MesajTrimisActionToolbar extends ActionToolbar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SpringBean
	private ApplicationService applicationService;

	private YesNoModalWindow deleteYesNoConfirmationModal;

	public MesajTrimisActionToolbar(MyDataTable<?> table) {
		super(table);

		deleteYesNoConfirmationModal = new YesNoModalWindow("delete.confirmation", "confirmare2") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onYes(AjaxRequestTarget target) {
				Mesaj k = (Mesaj) getTable().getSelectedRow().getModelObject();
				applicationService.removeMesaj(k);
			}

			@Override
			public void onNo(AjaxRequestTarget target) {

			}
		};

		deleteYesNoConfirmationModal.setOutputMarkupPlaceholderTag(true);

		add(deleteYesNoConfirmationModal);
		setOutputMarkupId(true);
	}

	@Override
	public List<ToolbarItem> getItemsList() {
		List<ToolbarItem> lista = new ArrayList<ToolbarItem>();
		lista.add(ToolbarItem.VIEW);
		lista.add(ToolbarItem.DELETE);

		return lista;
	}

	@Override
	public void onAction(ToolbarItem toolbarItem, AjaxRequestTarget target) {

		switch (toolbarItem) {
		case DELETE:
			onDeleteAction(target);
			break;
		case VIEW:
			onViewAction(target);
			break;
		}
	}

	private void onViewAction(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			ViewMesajPanel detailsPanel = new ViewMesajPanel("panel", new Model<Mesaj>((Mesaj) getTable()
					.getSelectedRow().getModelObject()), this.getPage().get("content:tabs:panel"));
			detailsPanel.setOutputMarkupId(true);
			Page page = this.getPage();

			page.get("content:tabs:panel").replaceWith(detailsPanel);

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onDeleteAction(AjaxRequestTarget target) {

		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			deleteYesNoConfirmationModal.show(target);

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}


	@Override
	public String getToolbarName(ToolbarItem toolbarItem) {
		return getString(toolbarItem.getName());
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

}
